package cn.edu.nju.software.android.layoutHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

import me.maxwin.view.IListviewCallback;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import cn.edu.nju.software.android.activity.R;
import cn.edu.nju.software.android.adapter.MessageListViewAdapter;
import cn.edu.nju.software.android.adapter.StatusListViewAdapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.TabHost;

public class MessagesLayoutHelper extends LayoutHelper implements
IXListViewListener, IListviewCallback {

	private XListView atMeCommentList;
	private XListView commentMeList;
	private XListView atMeStatusList;
	private StatusListViewAdapter atMeStatusListViewAdapter;
	private MessageListViewAdapter atMeCommentListViewAdapter;
	private MessageListViewAdapter commentMeListViewAdapter;

	private TabHost tabHost;
	private String tabTag_atMeComment;
	private String tabTag_commentMe;
	private String tabTag_atMeStatus;

	private Handler mHandler;

	public MessagesLayoutHelper(Context context) {
		super(context);
	}

	@Override
	public void initialize(View view) {
		atMeCommentList = (XListView) view.findViewById(R.id.at_me_comment_list);
		commentMeList = (XListView) view.findViewById(R.id.comment_me_list);
		atMeStatusList = (XListView)view.findViewById(R.id.at_me_weibo_list);
		mHandler = new Handler();
		atMeStatusList.setXListViewListener(this);
		atMeStatusList.setPullLoadEnable(false);
		atMeCommentList.setXListViewListener(this);
		atMeCommentList.setPullLoadEnable(false);
		commentMeList.setXListViewListener(this);
		commentMeList.setPullLoadEnable(true);
		setupTabInfomation(view);
	}

	private void setupTabInfomation(View view) {
		tabTag_atMeComment = context.getResources().getString(R.string.tab_tag_atMeComment);
		tabTag_commentMe = context.getResources().getString(
				R.string.tab_tag_commentMe);
		tabTag_atMeStatus = context.getResources().getString(R.string.tab_tag_atMeStatus);
		tabHost = (TabHost) view.findViewById(android.R.id.tabhost);
		tabHost.setup();
		tabHost.addTab(tabHost
				.newTabSpec(tabTag_atMeStatus)
				.setIndicator(
						context.getResources().getString(
								R.string.tab_text_atMeStatus))
								.setContent(R.id.at_me_weibo_list));
		tabHost.addTab(tabHost
				.newTabSpec(tabTag_atMeComment)
				.setIndicator(
						context.getResources()
						.getString(R.string.tab_text_atMeComment))
						.setContent(R.id.at_me_comment_list));
		tabHost.addTab(tabHost
				.newTabSpec(tabTag_commentMe)
				.setIndicator(
						context.getResources().getString(
								R.string.tab_text_commentMe))
								.setContent(R.id.comment_me_list));
		atMeCommentListViewAdapter = new MessageListViewAdapter(context, this,
				MessageListViewAdapter.AT_ME);
		commentMeListViewAdapter = new MessageListViewAdapter(context, this,
				MessageListViewAdapter.COMMENT_ME);
		atMeStatusListViewAdapter = new StatusListViewAdapter(context, this);
		atMeCommentList.setAdapter(atMeCommentListViewAdapter);
		commentMeList.setAdapter(commentMeListViewAdapter);
		atMeStatusList.setAdapter(atMeStatusListViewAdapter);
		atMeCommentList.setItemsCanFocus(true);
		commentMeList.setItemsCanFocus(true);
		atMeStatusList.setItemsCanFocus(true);
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public void onComplete() {
		String currentTabTag = tabHost.getCurrentTabTag();
		Date timeNow = new Date();
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		String time = format.format(timeNow);
		if (currentTabTag.equals(tabTag_atMeComment)) {
			atMeCommentList.stopRefresh();
			atMeCommentList.stopLoadMore();
			atMeCommentList.setRefreshTime(time);
		}
		if (currentTabTag.equals(tabTag_commentMe)) {
			commentMeList.stopRefresh();
			commentMeList.stopLoadMore();
			commentMeList.setRefreshTime(time);
		}
		if(currentTabTag.equals(tabTag_atMeStatus)){
			atMeStatusList.stopRefresh();
			atMeStatusList.stopLoadMore();
			atMeStatusList.setRefreshTime(time);
		}
	}

	@Override
	public void disableLoadFunction() {
		String currentTabTag = tabHost.getCurrentTabTag();
		if (currentTabTag.equals(tabTag_atMeComment)) {
			atMeCommentList.setPullLoadEnable(false);
		}
		if (currentTabTag.equals(tabTag_commentMe)) {
			commentMeList.setPullLoadEnable(false);
		}
		if(currentTabTag.equals(tabTag_atMeStatus)){
			atMeStatusList.setPullLoadEnable(false);
		}
	}

	@Override
	public void enableLoadFunction() {
		String currentTabTag = tabHost.getCurrentTabTag();
		if (currentTabTag.equals(tabTag_atMeComment)) {
			atMeCommentList.setPullLoadEnable(true);
		}
		if (currentTabTag.equals(tabTag_commentMe)) {
			commentMeList.setPullLoadEnable(true);
		}
		if(currentTabTag.equals(tabTag_atMeStatus)){
			atMeStatusList.setPullLoadEnable(true);
		}
	}

	@Override
	public void onRefresh() {
		String currentTabTag = tabHost.getCurrentTabTag();
		if (currentTabTag.equals(tabTag_atMeComment)) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					atMeCommentListViewAdapter.getNewestComments();
				}
			}, 2000);
		}
		if (currentTabTag.equals(tabTag_commentMe)) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					commentMeListViewAdapter.getNewestComments();
				}
			}, 2000);
		}
		if (currentTabTag.equals(tabTag_atMeStatus)) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					atMeStatusListViewAdapter.getNewestStatuses();
				}
			}, 2000);
		}
	}

	@Override
	public void onLoadMore() {
		String currentTabTag = tabHost.getCurrentTabTag();
		if (currentTabTag.equals(tabTag_atMeComment)) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					atMeCommentListViewAdapter.getMoreComments();
				}
			}, 2000);
		}
		if (currentTabTag.equals(tabTag_commentMe)) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					commentMeListViewAdapter.getMoreComments();
				}
			}, 2000);
		}
		if (currentTabTag.equals(tabTag_atMeStatus)) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					atMeStatusListViewAdapter.loadMoreStatuses();
				}
			}, 2000);
		}
	}

	@Override
	public void resetToTopRow() {
		// TODO Auto-generated method stub

	}

}
